package com.yuehuanghun.framework.service.location.provider;

import com.yuehuanghun.framework.json.JSON;
import com.yuehuanghun.framework.json.JSONObject;
import com.yuehuanghun.framework.service.location.AbstractIpLocationLimitRateQuerier;
import com.yuehuanghun.framework.util.StringUtils;
import com.yuehuanghun.framework.util.http.HttpUtils;

/**
 * 太平洋电脑网IP所在地查询
 * @author yadmin
 *
 */
public class PconlineIpLocationQuerier extends AbstractIpLocationLimitRateQuerier {
	private final static int PERMITS_PER_SECOND = 10; //未测试最大值，如果有必要可以调大
	// IP地址查询
	private static final String QUERY_URL = "http://whois.pconline.com.cn/ipJson.jsp";

	public PconlineIpLocationQuerier() {
		super(PERMITS_PER_SECOND);
	}

	@Override
	protected String[] query(String ipAddr) throws Exception {
		String rspStr = HttpUtils.sendGet(QUERY_URL, "ip=" + ipAddr + "&json=true", "GBK");
		if (StringUtils.isEmpty(rspStr)) {
			return null;
		}
		JSONObject obj = JSON.parseObject(rspStr);
		String province = obj.getString("pro");
		String city = obj.getString("city");
		return new String[] {province, city};
	}

}
